package com.cienet.playandrecord;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.RecordControl;

import mypackage.EchoScreen;

public class RecordSide extends Thread
{
    
    private Player mRecorder;
    
    private ByteArrayOutputStream mByteArrayOutputStream;
    
    private RecordControl mRecordControl;
    
    private int mRecordCount = 2;
    
    private boolean mRuning = true;
    
    public void run()
    {
        while (mRuning)
        {
            try
            {
                EchoScreen.log("init to record");
                mByteArrayOutputStream = new ByteArrayOutputStream();
                
                mRecorder = Manager.createPlayer("capture://audio?encoding=audio/amr");
                mRecorder.realize();
                
                mRecordControl = (RecordControl) mRecorder.getControl("RecordControl");
                mRecordControl.setRecordStream(mByteArrayOutputStream);
                mRecordControl.startRecord();
                
                EchoScreen.log("begin to record");
                mRecorder.start();
                
                sleep(1000);
                
                stopRecoding();
                
                
                if (mRecordCount > 0)
                {
                    mRecordCount--;
                    VoiceManager.prepare();   
                }
                else if (mRecordCount == 0)
                {
                    mRecordCount = -1;
                    VoiceManager.startPlay();
                }
                
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (MediaException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
    private void stopRecoding()
    {
        
        try
        {
            mRecordControl.commit();
            
            BufferManaer.setRecordBuffer(mByteArrayOutputStream.toByteArray());
            mByteArrayOutputStream.close();
            
            mRecorder.close();
            EchoScreen.log("mRecorder.close();");
            
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    
    public void stop()
    {
        mRuning = false;
        stopRecoding();
    }
    
    PlayerListener mRecordListener = new PlayerListener()
    {
        public void playerUpdate(Player arg0, String event, Object eventData)
        {
        }
    };
    
}
